package com.andy.service.impl;

import com.andy.infrastructure.util.FileUtil;
import com.andy.infrastructure.util.FreeMarkerUtil;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;

@Service
public class AbiCreateControllerService {

    @Autowired
    private freemarker.template.Configuration cfg;

    @Autowired
    private FreeMarkerUtil freeMarkerUtil;

    @Value("${myClasspath}")
    private String myClasspath;

    private static final String TOTLE_FILE_PATH = "D:\\autoOutput";

    //首次开发分文件夹，省去大批量的拷贝
    private static final String CONTROLLER_FILE_PATH = TOTLE_FILE_PATH+"\\controller";
    private static final String SERVICE_FILE_PATH = TOTLE_FILE_PATH+"\\service";
    private static final String SERVICE_IMPL_FILE_PATH = TOTLE_FILE_PATH+"\\service\\impl";
    private static final String REQUEST_FILE_PATH = TOTLE_FILE_PATH+"\\request";
    private static final String RESPONSE_FILE_PATH = TOTLE_FILE_PATH+"\\response";

    //正常不要分文件夹
//    private static final String CONTROLLER_FILE_PATH = TOTLE_FILE_PATH;
//    private static final String SERVICE_FILE_PATH = TOTLE_FILE_PATH;
//    private static final String SERVICE_IMPL_FILE_PATH = TOTLE_FILE_PATH;
//    private static final String REQUEST_FILE_PATH = TOTLE_FILE_PATH;
//    private static final String RESPONSE_FILE_PATH = TOTLE_FILE_PATH;

    /**
     * 创建百威controller、出入参、service
     * @param input
     * @return
     */
    public void createAbiControllerClass(String input){
        //创建controller
        doCreateAbiControllerClass(input);
        //创建service
        doCreateAbiServiceClass(input);
        //创建serviceImpl
        doCreateAbiServiceImplClass(input);
        //request
        doCreateAbiRequest(input);
        //response
        doCreateAbiResponese(input);

        //弹出框体
        FileUtil.popupDir(TOTLE_FILE_PATH);
    }


    /**
     * 创建controller
     * @param input
     */
    private void doCreateAbiControllerClass(String input){
        //1-class的内容
        String ftlName = "abi_controller.ftl";
        String ftlString = freeMarkerUtil.getFreeMarkerString(cfg, ftlName, input);
        //生成文件
        Map<String, Object> map =  freeMarkerUtil.putInput(input);
        String fileName = (String)map.get("controllerName_Iii")+"Controller.java";
        String filePath = CONTROLLER_FILE_PATH;
        try {
            //创建文件
            FileUtil.createFile(fileName,ftlString,filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建service接口
     * @param input
     */
    private void doCreateAbiServiceClass(String input){
        //1-class的内容
        String ftlName = "abi_service.ftl";
        String ftlString = freeMarkerUtil.getFreeMarkerString(cfg, ftlName, input);
        //生成文件
        Map<String, Object> map =  freeMarkerUtil.putInput(input);
        String fileName = (String)map.get("controllerName_Iii")+"Service.java";
        String filePath = SERVICE_FILE_PATH;
        try {
            //创建文件
            FileUtil.createFile(fileName,ftlString,filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void doCreateAbiServiceImplClass(String input){
        //1-class的内容
        String ftlName = "abi_serviceImpl.ftl";
        String ftlString = freeMarkerUtil.getFreeMarkerString(cfg, ftlName, input);
        //生成文件
        Map<String, Object> map =  freeMarkerUtil.putInput(input);
        String fileName = (String)map.get("controllerName_Iii")+"ServiceImpl.java";
        String filePath = SERVICE_IMPL_FILE_PATH;
        try {
            //创建文件
            FileUtil.createFile(fileName,ftlString,filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void doCreateAbiRequest(String input){
        try {
            String ftlName = "abi_request.ftl";
            //生成文件
            Map<String, Object> map =  freeMarkerUtil.putInput(input);
            List<Map<String, Object>> inputList = (List<Map<String, Object>>)map.get("methodList");

            for(Map<String, Object> tempMap:inputList){
                tempMap.put("classpath",myClasspath);
                Template t = cfg.getTemplate(ftlName);
                // 输出到控制台
                StringWriter writer = new StringWriter();
                t.process(tempMap, writer);
                String ftlString = writer.toString();

                String fileName = (String)tempMap.get("t2_Iii")+"Req.java";
                String filePath = REQUEST_FILE_PATH;
                //创建文件
                FileUtil.createFile(fileName,ftlString,filePath);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
    }

    private void doCreateAbiResponese(String input){
        try {
            String ftlName = "abi_response.ftl";
            //生成文件
            Map<String, Object> map =  freeMarkerUtil.putInput(input);
            List<Map<String, Object>> inputList = (List<Map<String, Object>>)map.get("methodList");

            for(Map<String, Object> tempMap:inputList){
                tempMap.put("classpath",myClasspath);
                Template t = cfg.getTemplate(ftlName);
                // 输出到控制台
                StringWriter writer = new StringWriter();
                t.process(tempMap, writer);
                String ftlString = writer.toString();

                String fileName = (String)tempMap.get("t2_Iii")+"Res.java";
                String filePath = RESPONSE_FILE_PATH;
                //创建文件
                FileUtil.createFile(fileName,ftlString,filePath);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
    }

}
